home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpse-2.1 / index / filetype.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-16  |  4.8 KB  |  174 lines

  1. /* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
  2. /* ./glimpse/index/filetype.c */
  3. /* --------------------------------------------------------------------------
  4.    this function detect whether a given file is of special type
  5.    which we do not want to index.
  6.    if so, then return(1) else return (0).
  7.    a file is said to be binary if more than 10% of character > 128
  8.    in the sampled input.
  9.    a file is a uuencoded file if (maybe after mail header), there is
  10.    a "begin" followed by 3 digits, and no lower case character.
  11.  
  12.    statistics we are concerned of:
  13.    1) average word length: should not be greater than 10.
  14.    2) index density: (the number of different words v.s. number of words).
  15.  
  16. -----------------------------------------------------------------------------*/
  17. #include "glimpse.h"
  18. #define SAMPLE_SIZE  8192
  19. #define WORD_THRESHOLD  18  /* the ratio between number of characters and
  20.         delimiters (blanks or \n) above which the file is determined to be
  21.         hqx or other non-natural language text */
  22.  
  23. #if    BG_DEBUG
  24. extern    FILE    *LOGFILE;
  25. #endif    /*BG_DEBUG*/
  26. char *member[MAX_4K_HASH];
  27. int member_tag[MAX_4K_HASH];
  28. int  file_id;
  29. extern  char *getword();
  30. extern char INDEX_DIR[MAX_LINE_LEN];
  31.  
  32. int
  33. filetype(name, dosuffix)
  34. char *name;
  35. int dosuffix;
  36. {
  37.     unsigned char buffer[SAMPLE_SIZE+1];
  38.     int num_read;
  39.         int BINARY=0;
  40.         int UUENCODED=0;
  41.     int fd;
  42.  
  43.     if (!dosuffix) goto nosuffix;
  44.     if (!strcmp(COMP_SUFFIX, &name[strlen(name)-strlen(COMP_SUFFIX)]))
  45.         return 0;
  46.     if (test_special_suffix(name)) {
  47. #if    BG_DEBUG
  48.         fprintf(LOGFILE, "special suffix: %s -- not indexing\n", name);
  49. #endif    /*BG_DEBUG*/
  50.         return 1;
  51.     }
  52.  
  53. nosuffix:
  54.     if((fd = open(name, 0)) < 0) {
  55.         /* This is the only thing the user might want to know: suppress other warnings */
  56.         fprintf(stderr, "permission denied or non-existent file: %s\n", name);
  57.         return(1);
  58.     }
  59.         if ((num_read = read(fd, buffer, SAMPLE_SIZE)) <= 0) {
  60. #if    BG_DEBUG
  61.         fprintf(LOGFILE, "no data: %s -- not indexing\n", name);
  62. #endif    /*BG_DEBUG*/
  63.         close(fd);
  64.         return 1;
  65.     }
  66.  
  67.     if (test_postscript(buffer, num_read)) {
  68. #if    BG_DEBUG
  69.         fprintf(LOGFILE, "postscript file: %s -- not indexing\n", name);
  70. #endif    /*BG_DEBUG*/
  71.         close(fd);
  72.         return 1;
  73.     }
  74.  
  75.         BINARY = test_binary(buffer, num_read);
  76.         if(BINARY == ON) {
  77. #if    BG_DEBUG
  78.         fprintf(LOGFILE, "binary file: %s -- not indexing\n", name);
  79. #endif    /*BG_DEBUG*/
  80.         close(fd);
  81.         return(1);
  82.     }
  83.  
  84.     /* now check for uuencoded file */
  85.         UUENCODED = test_uuencode(buffer, num_read);
  86.         if(UUENCODED == ON) {
  87. #if    BG_DEBUG
  88.         fprintf(LOGFILE, "uuencoded file: %s -- not indexing\n", name);
  89. #endif    /*BG_DEBUG*/
  90.         close(fd);
  91.         return(1);
  92.     }
  93.     if(heavy_index(name, buffer, num_read)) { 
  94. #if    BG_DEBUG
  95.         fprintf(LOGFILE, "heavy index file: %s -- not indexing\n ", name);
  96. #endif    /*BG_DEBUG*/
  97.         close(fd);
  98.         return(1);
  99.     }
  100.     if(hqx(name, buffer, num_read)) { 
  101. #if    BG_DEBUG
  102.         fprintf(LOGFILE, "too few real words: %s -- not indexing\n", name);
  103. #endif    /*BG_DEBUG*/
  104.         close(fd);
  105.         return(1);
  106.     }
  107.     close(fd);
  108.     return(0);
  109. }
  110.  
  111. /* ----------------------------------------------------------------------
  112.    check for heavy index file.
  113.    the function first test block 1 (of SAMPLE_SIZE bytes).
  114.    the file is determined to be heavy index file if
  115.    index_ratio > 0.9 and num_words > 500
  116.    ???
  117. ---------------------------------------------------------------------- */
  118. heavy_index(name, buffer, num_read)
  119. char *name;
  120. char *buffer;
  121. int num_read;
  122. {
  123.     char *buffer_end;
  124.     int hash_value;
  125.     int new_word_num=0;
  126.     int word_num=0;
  127.     char word[256];
  128.  
  129.     buffer_end = &buffer[num_read];
  130.     while((buffer = getword(word, buffer, buffer_end, NULL)) < buffer_end) {
  131.         if(word[0] == '\0') continue;
  132.         word_num++;
  133.         hash_value = hash4k(word, strlen(word));
  134.         if(member_tag[hash_value] != file_id) {
  135.             new_word_num++;
  136.             member_tag[hash_value] = file_id;
  137.         }
  138.     }
  139.     if(new_word_num * 100 >= word_num * 83 && word_num >= 500) return(1);
  140. #ifdef debug
  141.     printf("%s: new_word_num=%d, word_num=%d\n", name, new_word_num, word_num);
  142. #endif
  143.     return(0);
  144. }
  145.  
  146. /* ----------------------------------------------------------------------
  147.    check for hqx encoded files or other files with long lines,
  148.    for example, postscript files, core files, and others.
  149.    the function first test block 1 (of SAMPLE_SIZE bytes).
  150.    the file is determined to be bad if the ratio of blanks or newlines
  151.    is too small.
  152. ---------------------------------------------------------------------- */
  153.  
  154. hqx(name, buffer, num_read)
  155. char *name;
  156. char *buffer;
  157. int num_read;
  158. {
  159. int i;
  160. char c;
  161. int sep=0;
  162.     if (num_read < 2048) return(0) ;
  163.     for (i=0; i < num_read ; i++) {
  164.         c=buffer[i];
  165.         if (c == '\n' || c == ' ' || c == '/') sep++;
  166.     /* the '/' is for list of file names, including .name_list. */
  167.     /* the \n is for lists of words, but should be excluded really so
  168.         that dictionaries are excluded */
  169.     }
  170.     if (!sep) return(1);
  171.     if (num_read/sep > WORD_THRESHOLD) return(1);
  172.         else return(0);
  173.